import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';

@Injectable()
export class GoodsCateService {
    constructor(@InjectModel('GoodsCate') private readonly goodsCateModel) {}
    async find() {
        return await this.goodsCateModel.aggregate([
            { $match: { pid: '0' } },
            {
                $lookup: {
                    localField: '_id',
                    foreignField: 'pid',
                    as: 'children',
                    from: 'goods_cate',
                    pipeline: [
                        {
                            $lookup: {
                                localField: 'pid',
                                foreignField: '_id',
                                from: 'goods_cate',
                                as: 'pid',
                            },
                        },
                        { $unwind: '$pid' },
                    ],
                },
            },
        ]);
    }
    async create(goodsCateInfo) {
        return await this.goodsCateModel.create(goodsCateInfo);
    }
    async update(goodsCateInfo) {
        return await this.goodsCateModel.updateOne({ _id: goodsCateInfo._id }, goodsCateInfo);
    }
    async delete(goodsCateInfo) {
        return await this.goodsCateModel.deleteOne(goodsCateInfo);
    }
    getModel() {
        return this.goodsCateModel;
    }
}
